package org.lord.slark.numbers.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.lord.slark.numbers.R;
import org.lord.slark.numbers.utils.LocalConstants;

import java.util.Calendar;

public class AddMeetingData extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {
    private static final int REQUEST_CODE = 100;
    private int mRequestNumType = 0;
    private ImageView btnback;
    private TextView btnsave;
    private Spinner spyear, spmonth, spweek;
    private EditText etnormalNum, etbaptizeNum, etmorningNum, etprayNum, etgospelNum, etsundayNum;
    private Calendar mCalendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_add_meeting_data);
        mCalendar = Calendar.getInstance();
        initView();
    }

    private void initView() {
        btnback = (ImageView) findViewById(R.id.btnback);
        btnsave = (TextView) findViewById(R.id.btnsave);
        etnormalNum = (EditText) findViewById(R.id.etnormalNum);
        etbaptizeNum = (EditText) findViewById(R.id.etbaptizeNum);
        etmorningNum = (EditText) findViewById(R.id.etmorningNum);
        etprayNum = (EditText) findViewById(R.id.etprayNum);
        etgospelNum = (EditText) findViewById(R.id.etgospelNum);
        etsundayNum = (EditText) findViewById(R.id.etsundayNum);
        spyear = (Spinner) findViewById(R.id.spyear);
        spmonth = (Spinner) findViewById(R.id.spmonth);
        spweek = (Spinner) findViewById(R.id.spweek);
        if (mCalendar.get(Calendar.YEAR) >= 2012) {
            spyear.setSelection(mCalendar.get(Calendar.YEAR) - 2012, true);
        }
        spmonth.setSelection(mCalendar.get(Calendar.MONTH), true);
        spweek.setSelection(mCalendar.get(Calendar.WEEK_OF_MONTH) - 1, true);
        btnback.setOnClickListener(this);
        btnsave.setOnClickListener(this);
        etnormalNum.setInputType(InputType.TYPE_NULL);
        etbaptizeNum.setInputType(InputType.TYPE_NULL);
        etmorningNum.setInputType(InputType.TYPE_NULL);
        etprayNum.setInputType(InputType.TYPE_NULL);
        etgospelNum.setInputType(InputType.TYPE_NULL);
        etsundayNum.setInputType(InputType.TYPE_NULL);
        etnormalNum.setOnFocusChangeListener(this);
        etbaptizeNum.setOnFocusChangeListener(this);
        etmorningNum.setOnFocusChangeListener(this);
        etprayNum.setOnFocusChangeListener(this);
        etgospelNum.setOnFocusChangeListener(this);
        etsundayNum.setOnFocusChangeListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnback:
                onBackPressed();
                break;
            case R.id.btnsave:
                //TODO, insert into DB
                Toast.makeText(this, "数据已保存！", Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * Called when the focus state of a view has changed.
     *
     * @param v        The view whose state has changed.
     * @param hasFocus The new focus state of v.
     */
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            switch (v.getId()) {
                case R.id.etnormalNum:
                    mRequestNumType = 1;
                    break;
                case R.id.etbaptizeNum:
                    mRequestNumType = 2;
                    break;
                case R.id.etgospelNum:
                    mRequestNumType = 3;
                    break;
                case R.id.etmorningNum:
                    mRequestNumType = 4;
                    break;
                case R.id.etprayNum:
                    mRequestNumType = 5;
                    break;
                case R.id.etsundayNum:
                    mRequestNumType = 6;
                    break;
                default:
                    break;
            }
            jumpToChooseSaints(mRequestNumType);
        }
    }

    private void jumpToChooseSaints(int requestId) {
        if (requestId == 0) return;
        Intent i = new Intent(this, ChooseSaints.class);
        i.putExtra(LocalConstants.REQUESTID, requestId);
        startActivityForResult(i, REQUEST_CODE);
    }

    /**
     * Dispatch incoming result to the correct fragment.
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            int requestID = data.getIntExtra(LocalConstants.REQUESTID, 1);
            int resultNum = data.getIntExtra(LocalConstants.RESULT_NUMBER, 0);
            switch (requestID) {
                case 1:
                    etnormalNum.setText(resultNum+"");
                    break;
                case 2:
                    etbaptizeNum.setText(resultNum+"");
                    break;
                case 3:
                    etgospelNum.setText(resultNum+"");
                    break;
                case 4:
                    etmorningNum.setText(resultNum+"");
                    break;
                case 5:
                    etprayNum.setText(resultNum+"");
                    break;
                case 6:
                    etsundayNum.setText(resultNum+"");
                    break;
                default:
                    break;
            }
        }
    }
}
